package com.knowvoyage.knowvoyageserver.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.mapper.MessageMapper;
import com.knowvoyage.knowvoyageserver.pojo.entity.Message;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {

    private final MessageMapper messageMapper;
    @Override
    public Result<Boolean> insert(Message message) {
        Long messageId = message.getMessageId();
        if (messageId == null) {
            return Result.error(400,"消息ID不能为空");
        }
        if (messageMapper.selectById(messageId) != null) {
            return Result.error(400,"消息已存在");
        }
        messageMapper.insert(message);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> deleteById(Long messageId) {
        if (messageId == null) {
            return Result.error(400,"消息ID不能为空");
        }
        if (messageMapper.selectById(messageId) == null) {
            return Result.error(400,"消息不存在");
        }
        messageMapper.deleteById(messageId);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> update(Message message) {
        Long messageId = message.getMessageId();
        if (messageId == null) {
            return Result.error(400,"消息ID不能为空");
        }
        if (messageMapper.selectById(messageId) == null) {
            return Result.error(400,"消息不存在");
        }
        messageMapper.update(message);
        return Result.success(true);
    }

    @Override
    public Result<Message> select(Long messageId) {
        if (messageId == null) {
            return Result.error(400,"消息ID不能为空");
        }
        return Result.success(messageMapper.selectById(messageId));
    }

    @Override
    public Result<List<Message>> selectByDialogId(Long dialogId) {
        if (dialogId == null) {
            return Result.error(400,"对话ID不能为空");
        }
        return Result.success(messageMapper.selectByDialogId(dialogId));
    }

    @Override
    public Result<Message> selectLastByDialogId(Long dialogId) {
        if (dialogId == null) {
            return Result.error(400,"对话ID不能为空");
        }
        return Result.success(messageMapper.selectLastByDialogId(dialogId));
    }

    @Override
    public Result<Integer> countByDialogId(Long dialogId) {
        if (dialogId == null) {
            return Result.error(400,"对话ID不能为空");
        }
        return Result.success(messageMapper.countByDialogId(dialogId));
    }

    @Override
    public Result<PageInfo<Message>> selectByDialogIdPage(Long dialogId, int page, int size) {
        try {
            // 设置分页参数
            PageHelper.startPage(page, size);

            // 查询数据
            List<Message> messages = messageMapper.selectByDialogId(dialogId);

            // 包装分页结果
            PageInfo<Message> pageInfo = new PageInfo<>(messages);

            if (messages.isEmpty()) {
                return Result.error(400,"该对话暂无消息或页码超出范围");
            }
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error(400,"分页查询失败: " + e.getMessage());
        } finally {
            // 清理ThreadLocal存储的分页参数
            PageHelper.clearPage();
        }
    }
}
